home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / deck.exe / DECK.CPP < prev    next >
C/C++ Source or Header  |  1991-07-21  |  6KB  |  184 lines

  1. //  Deck class - A class for handling a deck of cards
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "deck.hpp"
  7.  
  8. /////////////////////////////////////////////////////////////////////////////
  9. // DECK() This constructor builds a deck of cards consisting of num decks
  10. // The default is one deck.  There is a crude error message if there isn't
  11. // enough memory to create the request number of decks.  Two sets of CARD
  12. // structs are set up.  This is for the Shuffle() routine.
  13. /////////////////////////////////////////////////////////////////////////////
  14. DECK::DECK(int num)
  15. {
  16.     char *suits[]={"Diamonds","Hearts","Spades","Clubs"};
  17.     char *cardname[]={"","","Deuce","Three","Four","Five","Six","Seven","Eight",
  18.                             "Nine","Ten","Jack","Queen","King","Ace"};
  19.     NumOfDecks=num;
  20.     NumOfCards=NumOfDecks*DECKCOUNT;
  21.     Deck=new CARD[NumOfCards];
  22.     StartDeck=new CARD[NumOfCards];
  23.     if(!Deck || !StartDeck)
  24.     {
  25.         puts("Not enough memory for cards!");
  26.         puts("Terminating program.");
  27.         exit(1);
  28.     }
  29.     int ctr=0;
  30.     for(int c=0;c<num;c++)
  31.     {
  32.         for(int i=0;i<4;i++)
  33.         {
  34.             for(int j=2;j<15;j++)
  35.             {
  36.                 StartDeck[ctr].value=j;            // Card value
  37.                 StartDeck[ctr].suit=i;            // Card suit
  38.                 StartDeck[ctr].graphic=ctr;    // Graphic image pointer
  39.                 if(j<11)
  40.                     StartDeck[ctr].face=j;     // Face value of card
  41.                 else
  42.                     if(j<14)
  43.                         StartDeck[ctr].face=10;
  44.                     else
  45.                         StartDeck[ctr].face=11;
  46.                                                         // Name of card
  47.                 sprintf(StartDeck[ctr++].name,"%s of %s",cardname[j],suits[i]);
  48.             }
  49.         }
  50.     }
  51.     SetCardsDealt(NOT_DEALT);
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // ~DECK()  The destructor just deletes the memory used by the CARD structs
  55. /////////////////////////////////////////////////////////////////////////////
  56. DECK::~DECK()
  57. {
  58.     delete Deck;
  59.     delete StartDeck;
  60. }
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // TakeNextCard() pulls the next card off the deck.  If it is the last card,
  64. // it updates private member int LastCard to TRUE.  This value can be tested
  65. // by calling IsLastCard.  This is good for blackjack games.  It also calls
  66. // Shuffle if needed.
  67. /////////////////////////////////////////////////////////////////////////////
  68. CARD DECK::TakeNextCard()
  69. {
  70.     NextCard++;
  71.     if(NextCard>NumOfCards)
  72.         Shuffle();
  73.     if(NextCard==NumOfCards)
  74.         SetLastCard(1);
  75.     return(Deck[NextCard]);
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // GetCardName returns a const char(so it can't be modified) of the name of
  80. // the card in the Deck at position loc.
  81. /////////////////////////////////////////////////////////////////////////////
  82. const char * DECK::GetCardName(int loc)
  83. {
  84.     if(loc<0 || loc>51)
  85.         return("");
  86.     else
  87.         return(Deck[loc].name);
  88. }
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // GetFaceValue returns the face value of the card at position loc.
  92. /////////////////////////////////////////////////////////////////////////////
  93. int DECK::GetFaceValue(int loc)
  94. {
  95.     if(loc<0 || loc>51)
  96.         return(-1);  //loc out of range
  97.     else
  98.         return(Deck[loc].face);
  99. }
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // GetValue returns the value of the card at position loc.
  103. /////////////////////////////////////////////////////////////////////////////
  104. int DECK::GetValue(int loc)
  105. {
  106.     if(loc<0 || loc>51)
  107.         return(-1);   // loc out of range
  108.     else
  109.         return(Deck[loc].value);
  110. }
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // GetSuit returns the suit of the card at position loc.
  114. /////////////////////////////////////////////////////////////////////////////
  115. int DECK::GetSuit(int loc)
  116. {
  117.     if(loc<0 || loc>51)
  118.         return(-1);   // loc out of range
  119.     else
  120.         return(Deck[loc].suit);
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // GetGraphic returns the graphic pointer of the card at position loc.
  125. /////////////////////////////////////////////////////////////////////////////
  126. int DECK::GetGraphic(int loc)
  127. {
  128.     if(loc<0 || loc>51)
  129.         return(-1);    // loc out of range
  130.     else
  131.         return(Deck[loc].graphic);
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // Shuffle shuffles the Deck of cards by randomly selecting a card from one
  136. // Deck(StartDeck) and placing it in the other.  This routine is based upon
  137. // one I found by Al Stevens in DDJ(I forget which volume and number) that
  138. // was used to shuffle a deck of cards for his blackjack game.
  139. /////////////////////////////////////////////////////////////////////////////
  140. void DECK::Shuffle()
  141. {
  142.     static int FirstTime=1;
  143.  
  144.     if(FirstTime)
  145.     {
  146.         NextCard=NumOfCards;
  147.         FirstTime=0;
  148.         randomize();
  149.     }
  150.     else
  151.     {
  152.         for(NextCard=0;NextCard<NumOfCards;NextCard++)
  153.             StartDeck[NextCard]=Deck[NextCard];
  154.     }
  155.     while(NextCard)
  156.     {
  157.         int i=random(NextCard);
  158.         NextCard--;
  159.         Deck[NextCard]=StartDeck[i];
  160.         while(i<NextCard)
  161.         {
  162.             StartDeck[i]=StartDeck[i+1];
  163.             i++;
  164.         }
  165.     }
  166. }
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CutDeck allows you to cut the deck starting with position loc.
  170. /////////////////////////////////////////////////////////////////////////////
  171. void DECK::CutDeck(int loc)
  172. {
  173.     if(loc>0 || loc<NumOfCards)
  174.     {
  175.         int ctr=0;
  176.         for(int i=loc;i<NumOfCards;i++)
  177.             StartDeck[ctr++]=Deck[i];
  178.         for(int j=0;j<loc;j++)
  179.             StartDeck[ctr++]=Deck[j];
  180.         for(int k=0;k<NumOfCards;k++)
  181.             Deck[k]=StartDeck[k];
  182.     }
  183. }
  184.